package com.windward.vii.dao.mapper;

import com.windward.vii.dao.entity.User;
import com.windward.vii.domain.user.SearchParam;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    @Delete({
        "delete from user",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into user (id, username, ",
        "mobile, password, ",
        "salt, third_type, ",
        "third_id, third_option, ",
        "last_login_ip, last_login_time, ",
        "register_time, register_ip, ",
        "last_login_device_id, im_token, ",
        "device_token)",
        "values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, ",
        "#{mobile,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, ",
        "#{salt,jdbcType=VARCHAR}, #{thirdType,jdbcType=TINYINT}, ",
        "#{thirdId,jdbcType=VARCHAR}, #{thirdOption,jdbcType=VARCHAR}, ",
        "#{lastLoginIp,jdbcType=VARCHAR}, #{lastLoginTime,jdbcType=TIMESTAMP}, ",
        "#{registerTime,jdbcType=TIMESTAMP}, #{registerIp,jdbcType=VARCHAR}, ",
        "#{lastLoginDeviceId,jdbcType=BIGINT}, #{imToken,jdbcType=VARCHAR}, ",
        "#{deviceToken,jdbcType=VARCHAR})"
    })
    int insert(User record);

    int insertSelective(User record);

    @Select({
        "select",
        "id, username, mobile, password, salt, third_type, third_id, third_option, last_login_ip, ",
        "last_login_time, register_time, register_ip, last_login_device_id, im_token, ",
        "device_token",
        "from user",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    User selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(User record);

    @Update({
        "update user",
        "set username = #{username,jdbcType=VARCHAR},",
          "mobile = #{mobile,jdbcType=VARCHAR},",
          "password = #{password,jdbcType=VARCHAR},",
          "salt = #{salt,jdbcType=VARCHAR},",
          "third_type = #{thirdType,jdbcType=TINYINT},",
          "third_id = #{thirdId,jdbcType=VARCHAR},",
          "third_option = #{thirdOption,jdbcType=VARCHAR},",
          "last_login_ip = #{lastLoginIp,jdbcType=VARCHAR},",
          "last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP},",
          "register_time = #{registerTime,jdbcType=TIMESTAMP},",
          "register_ip = #{registerIp,jdbcType=VARCHAR},",
          "last_login_device_id = #{lastLoginDeviceId,jdbcType=BIGINT},",
          "im_token = #{imToken,jdbcType=VARCHAR},",
          "device_token = #{deviceToken,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(User record);

    /***********以下为自定义方法************************/
    @Select({
            "select * ",
            "from user",
            "where mobile = #{mobile,jdbcType=VARCHAR}"
    })
    @ResultMap("BaseResultMap")
    User selectByMobile(String mobile);

    @Select({
            "select * ",
            "from user",
            "where third_id = #{thirdId,jdbcType=VARCHAR}"
    })
    @ResultMap("BaseResultMap")
    User selectByThirdId(String thirdId);

    int searchCount(@Param("param") SearchParam param);

    List<User> getNewestUserList();

    List<User> getListByIds(List<Long> ids);

    List<Long> getOnlineUserIds(@Param("sex") Integer sex);

    @Update({
            "update user",
            "set online_status = #{onlineStatus,jdbcType=TINYINT}",
            "where id = #{anchorUid,jdbcType=BIGINT}"
    })
    int updateOnlineStatusById(@Param("anchorUid") Long anchorUid, @Param("onlineStatus") Integer onlineStatus);

}